package com.api.controller;

import com.api.mapper.HealthRecordMapper;
import com.api.mapper.HealthScoreMapper;
import com.api.pojo.HealthRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/hr")
public class HealthRecordController {

    @Autowired
    HealthRecordMapper hrm;

    @Autowired
    HealthScoreMapper hsm;

    @RequestMapping("/create")
    public Map<String, Object> create(HealthRecord hr) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        hrm.create(hr);
        List<Integer> list = hrm.getChamberIds();
        list.forEach(e -> {
            hsm.create(hr.getId(), e);
        });
        map.put("data", true);
        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    @RequestMapping("/remove")
    public Map<String, Object> remove(Integer id) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        hrm.remove(id);
        hsm.remove(id);
        map.put("data", true);
        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    @RequestMapping("/update")
    public Map<String, Object> update(HealthRecord hr) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        hrm.update(hr);
        map.put("data", true);
        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    @RequestMapping("/get")
    public Map<String, Object> get(Integer pageNo, Integer pageSize) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        pageNo = --pageNo * pageSize;
        map.put("data", hrm.get(pageNo, pageSize));
        map.put("total", hrm.count());
        map.put("code", code);
        map.put("msg", msg);
        return map;
    }
}
